/**
 * Created by Ryze on 15/12/16.
 */
define(function() {
// 模块代码s

var rnetworking = {
    create: function () {
        var dat1a = {};
        dat1a.json = {};
        str1ing = "";
        dat1a.addParams = function (key, value) {
            dat1a.json[key] = value;
            return dat1a;
        };
        dat1a.toString = function () {
            return function (o) {
                var arr = [];
                var fmt = function (s) {
                    if (typeof s == 'object' && s != null) return  this.obj2string(s);
                    return /^(string|number)$/.test(typeof s) ? "\"" + s + "\"" : s;
                }
                for (var i in o) arr.push("\"" + i + "\":" + fmt(o[i]));
                return '{' + arr.join(',') + '}';
            }(dat1a.json);
        };
        dat1a.toJson = function () {
            return dat1a.json;
        };

        dat1a.ajax = function (url, successfun, errorfun, timeout) {
            fajax(url,"json", dat1a.toJson(), function (data) {
                //TODO  Success
                successfun(data);
            }, function () {
                //TODO error
                errorfun();
            }, timeout ? timeout : 30000);
        };
        return dat1a;
    }, createRSA: function () {
        var DEFAULT_PUBLIC_KEY =
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvFXRggdm8J+2w4frQqyRh0nL4G8Pgus9MOi8J+CrcLS0wXC0K7PlgL7MAaIFbHxRRlEZ5lw9iufteQHuMeXjKnAWE44c2Dyar9iq1p8Zz6ZaBaaDgCJUJi0Fv+CGPQnoWjbgdjMZXWF1Vutc1jxC9WmmQHZxKK4VQMbkZT7FwUwIDAQAB";
        var DEFAULT_PRIVATE_KEY =
            "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAK8VdGCB2bwn7bDh+tCrJGHScvgbw+C6z0w6Lwn4KtwtLTBcLQrs+WAvswBogVsfFFGURnmXD2K5+15Ae4x5eMqcBYTjhzYPJqv2KrWnxnPploFpoOAIlQmLQW/4IY9CehaNuB2MxldYXVW61zWPEL1aaZAdnEorhVAxuRlPsXBTAgMBAAECgYEAnlYEvRIxWtuxNXnoXbWoXV744jAZ4K6QshscVyO7fwrMByQVCABvNfFtLD1WXpWoxm4VnfhizaHsFbyy/sML3JAlHD8V/6cSWN89hg/NKjn+pGOoDlw/GHkOm45NTeF+D28u+e/EQvFrFnod4JobhI3xJklGAEPRlnLNLAOfgUECQQDcm0b1ZzpFJkr0jM2ReYHLtIx77a7C+R5vrhENeqF0KlYPlZ4BagEPrKr9boWfT4Pu5u3UQGm2JSnZnHzq4A9JAkEAyyx5LHg1KbFRS1RQ+QkxLtzVE4S1GPmg0GNfEXQZDnrxCXeM3uerAVwp0947NcwLtvz1EdPyzJ5kAXeMGQYWuwJAdYnuBraXX1yFt0KGDKp2s3hhGW6MHKnqw4o037+k4mXkMK2A4DQAmL9g42LNoxIUs2cq9FnVeQzX/W/ZRrNWsQJAfoaiq5Psn5tCYiQPDRexXlq+Axq44OXMRRotQSRI2mCrrNabjtE9CGrJzZxuyTDZ9soPIFwar1zQMK2vPIIzMwJBALNmLN0hdxnC/0j8PNj8XyK0AuoR+d3wGfkhKyb/5e2bHoXvx47jGGfMYvAmxg+kfhGu/Ls9S/M9pLKHirSfao4=";


        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(DEFAULT_PUBLIC_KEY);
        var decrypt = new JSEncrypt();
        decrypt.setPrivateKey(DEFAULT_PRIVATE_KEY);

        var dat1a = {};
        dat1a.json = {};
        str1ing = "";
        dat1a.addParams = function (key, value) {
            dat1a.json[key] = value;
            return dat1a;
        };
        dat1a.toString = function () {
            return function (o) {
                var arr = [];
                var fmt = function (s) {
                    if (typeof s == 'object' && s != null) return  this.obj2string(s);
                    return /^(string|number)$/.test(typeof s) ? "\"" + s + "\"" : s;
                }
                for (var i in o) arr.push("\"" + i + "\":" + fmt(o[i]));
                return '{' + arr.join(',') + '}';
            }(dat1a.json);
        };
        dat1a.toJson = function () {
            return  encrypt.encrypt(dat1a.toString());
        };

        dat1a.ajax = function (url, successfun, errorfun, timeout) {
            fajax(url,"text", dat1a.toJson(), function (data) {
                var uncrypted = decrypt.decrypt(data);
                uncrypted = decodeURIComponent(uncrypted);
                //TODO  Success
                successfun($.parseJSON(uncrypted));
            }, function () {
                //TODO error
                errorfun();
            }, timeout ? timeout : 30000);
        };
        return dat1a;
    }
};

var fajax = function (url, dateType,params, succ, err, timeout) {
    ajax({"url": url,"dateType":dateType, "param": params, "success": succ, "error": err,"time":timeout});
};
var ajax = function (p, $t) {
    $.ajax({
       // type: 'GET',
        dataType: p.dateType?p.dateType:"json",
        contentType: (p.dateType=="json")?"application/x-www-form-urlencoded":"application/json",
        url: p.url,
        data: p.param ? p.param : "",
        timeout: p.time ? p.time : 3000,
        beforeSend: function () {
            console.log("请求：" + p.url + "  参数：");
            console.log(p.param);
            if (p.beforeSend) {
                p.beforeSend();
            }
        }
    }).fail(function () {
        console.log("请求失败：" + p.url + "   参数：");
        console.log(p.param);
//            alert("请求失败");
        if (p.error) {
            p.error();
        }
    }).done(function (data) {
        console.log("请求成功：" + p.url + "  返回值：");
        console.log(data);

        if ($t) {
//                    $t.data("data", data);    //数据持久化
        }
        if (p.success) {
            p.success(data);
        }
    });
};


    return rnetworking;
});